﻿<?php

	class Controller_Rights extends Controller_Base
	{
		function index()
		{
			$roles = WebApp::app()->db->select(array('*'),array('maskitem'),'type = '.Rights::TYPE_ROLE);
			$this->assignData('roles',$roles);
		}
		function view()
		{
			$masks = WebApp::app()->db->select(array('maskitemchild.child'),array('maskitem', 'maskitemchild'), 'maskitem.name = "'.
			$this->args[0].'" AND maskitem.name = maskitemchild.parent');
			
			$sql = 'type = '.Rights::TYPE_ACTION;
			foreach($masks as $m)
			{
				$sql .= ' AND maskitem.name <> "'.$m->child.'"';
			}
			$masks2 = WebApp::app()->db->select(array('maskitem.name'),array('maskitem'),$sql);
			
			$this->assignData('assignMasks',$masks);
			$this->assignData('notassignMasks',$masks2);
			$this->assignData('role',$this->args[0]);
		}
		
		function add()
		{
			if(isset($this->args[0]) && isset($this->args[1]))
			{
				$cP = WebApp::app()->db->select(array('*'),array('maskitem'),'maskitem.name = "'.$this->args[0].'"');
				$cC = WebApp::app()->db->select(array('*'),array('maskitem'),'maskitem.name = "'.$this->args[1].'"');
				if(count($cP) > 0 && count($cC) > 0)
				{				
					WebApp::app()->db->insert('maskitemchild',array(
						'parent' => $this->args[0],
						'child' => $this->args[1],
					));
				}
			}
			header("Location:" . WebApp::app()->getBaseUrl() . 'rights/view/'. $this->args[0]);
			exit;
		}
		function delete()
		{
			if(isset($this->args[0]) && isset($this->args[1]))
			{
				WebApp::app()->db->delete('maskitemchild', 'parent = "'.$this->args[0].'" AND child = "'.$this->args[1].'"');
			}
			header("Location:" . WebApp::app()->getBaseUrl() . 'rights/view/'. $this->args[0]);
			exit;
		}
		
		function addrole()
		{
			if(isset($_POST['rolename']))
			{
				$tmp = WebApp::app()->db->select(array('*'),array('maskitem'),'maskitem.name = "'.$_POST['rolename'].'"');
				if(count($tmp) == 0){
					$descr = isset($_POST['roledescr']) ? $_POST['roledescr'] : '';
					WebApp::app()->db->insert('maskitem', array(
						'name' => $_POST['rolename'],
						'descr' => $descr,
						'type' => Rights::TYPE_ROLE,
					));
					header("Location:" . WebApp::app()->getBaseUrl() . 'rights/view/'. $_POST['rolename']);
					exit;
				}
				else
				{
					$this->assignData('msg', 'Такое название роли недоступно!');
				}
			}
			
		}
		function deleterole()
		{
			WebApp::app()->db->delete('maskitem','maskitem.name = "'.$this->args[0].'"');
			header("Location:" . WebApp::app()->getBaseUrl() . 'rights');
			exit;
		}
		
		function managerights()
		{
			$f = WebApp::app()->router->path;
			$f = strstr($f, 'administrator' , true);
			$f .= 'controllers\\';
			$dir = opendir($f);
			$files = array();
			while($file  = readdir($dir))
			{
				if($file != '.' && $file != '..' && strstr($file, '.php'))
				{
					$files[] = $file;
				}
			}
			closedir($dir);
			
			$tmp = array();
			$maskitem = WebApp::app()->db->select(array('name'),array('maskitem'));
			
			foreach($files as $file)
			{
				$r = false;
				foreach($maskitem as $mm)
				{
					if($mm->name == strstr($file,'.php',true).'.*')
					{
						$r = true;
						break;
					}
				}
				if(!$r)
	 				$tmp[] = strstr($file, '.php', true) . '.*';
			}
			//////////////
			$text = array();
			foreach($files as $file)
			{
				$fh = fopen($f.$file,'r');
		
				$fname = substr($file,0,strpos($file,'.php')).'.';
				while(!feof($fh))
				{
					$str = fgets($fh);
					if(strpos($str,'function') > -1){
						$tmp_f = substr($str,strpos($str,'function ')+strlen('function '),strlen($str)-strlen('function '));
						$tmp_f = $fname.substr($tmp_f, 0, strpos($tmp_f ,'('));
						$text[] = $tmp_f;
					}
				}
				
				fclose($fh);//Закрываем файловый дескриптор
			}
			$actions = array();
			$checker = false;
			foreach($text as $t)
			{
				$checker = false;
				foreach($maskitem as $mm)
				{
					if($t == $mm->name){
						$checker = true;
						break;
					}
				}
				if(!$checker)
					$actions[] = $t;
			}
			/////////////
			
			$this->assignData('controllers', $tmp);
			$this->assignData('actions', $actions);
		}
		
		function addrightcontroller()
		{
			WebApp::app()->db->insert('maskitem', array(
				'name' => $this->args[0],
				'descr' => '',
				'type' => Rights::TYPE_ACTION,
			));
			header("Location:" . WebApp::app()->getBaseUrl() . 'rights/managerights');
			exit;
		}
	}

?>